<?php
/**
 * Article
 *
 * @author Ferman Yee <fermanyee@163.com>
 * @copyright 2018-11-6
 */

namespace XppOpenApi\Core;

class Spider extends ApiBase {

    /**
     * 生成链接模型
     * @param array $urls   // 链接数量不能小于2
     * <code>
     * array(
     *      'http://www.example.com',
     *      'http://www.example.com',
     *      'http://www.example.com',
     * )
     * </code>
     * @return array
     * <code>
     * array(
     *      'status'    => 200,
     *      'info'      => '',
     *      'data'      => array(
     *          'pattern'       => 'http://www.example.com/pattern',
     *      ),
     * );
     * </code>
     * <b>status的值</b>
     * - 200:   成功
     * - -1:    参数错误
     */
    public function buildUrlPattern($urls) {
        return $this->request($this->getApiUrl('Spider', '/Spider/buildUrlPattern'), array(
            'urls' => $urls,
        ));
    }
    
    /**
     * 添加爬虫任务
     * @param string $url
     * <code>
     * http://www.example.com
     * </code>
     * @param string $notify_url
     * <code>
     * http://www.example.com/notify.php
     * </code>
     * @param array $options
     * <code>
     * array(
     *      'max_crawl_count' => 1,
     *      'is_inc' => true|false, //是否增量采集，默认false
     *      'delay' => 150, //请求延时时间，单位毫秒，默认150毫秒，不能小于150毫秒
     *      'rules' => array(
     *          'list_url_patterns' => array(),
     *          'page_url_patterns' => array(),
     *      ),
     *      'attach' => '', // 附加参数，原样返回
     * )
     * </code>
     * @return array
     * array(
     *      'status'    => 200,
     *      'info'      => '',
     *      'data'      => array(
     *      ),
     * );
     * 
     * @desc 异步回调地址接收的数据，json格式
     * <code>
     * array(
     *      'status'    => 200,
     *      'info'      => '',
     *      'data'      => array(
     *          'app_id'        => '',
     *          'access_token'  => '',
     *          'timestamp'     => 1542871501,
     *          'nonce'         => '',
     *          'finished'      => true|false,   // finished 为true时，title、keywords、description、content等没有内容
     *          'title'         => '',
     *          'keywords'      => '',
     *          'description'   => '',
     *          'content'       => '',
     *          'attach'        => '',
     *          'sign'          => '',
     *      ),
     * );
     * </code>
     * <b>status的值</b>
     * - 200:   成功
     * - -1:    参数错误
     */
    public function crawlArticles($url, $notify_url, $options = array()) {
        return $this->request($this->getApiUrl('Spider', '/Spider/crawlArticles'), array_merge(array(
            'url' => $url,
            'notify_url' => $notify_url
        ), (array) $options));
    }
    
    /**
     * 获取页面列表链接，智能分组
     * @param string $url
     * <code>
     * http://www.example.com
     * </code>
     * @return array
     * <code>
     * array(
     *      'status'    => 200,
     *      'info'      => '',
     *      'data'      => array(
     *          'groups' => array(
     *              array(
     *                  'type' => array('normal', 'list', 'page'),
     *                  'result' => array(
     *                      'url' => '',
     *                      'text' => ''
     *                  )
     *              )
     *          ),
     *          'count' => 10
     *      ),
     * );
     * </code>
     * <b>status的值</b>
     * - 200:   成功
     * - -1:    参数错误
     */
    public function autoList($url) {
        return $this->request($this->getApiUrl('Spider', '/Spider/autoList'), array(
            'url' => $url,
        ));
    }
    
    /**
     * 智能提取正文内容
     * @param string $url
     * <code>
     * http://www.example.com
     * </code>
     * @param string $title
     * <code>
     * article's title
     * </code>
     * @return array
     * <code>
     * array(
     *      'status'    => 200,
     *      'info'      => '',
     *      'data'      => array(
     *          'title' => '',
     *          'keywords' => '',
     *          'description' => '',
     *          'content' => '',
     *      ),
     * );
     * </code>
     * <b>status的值</b>
     * - 200:   成功
     * - -1:    参数错误
     */
    public function autoArticle($url, $title = '') {
        return $this->request($this->getApiUrl('Spider', '/Spider/autoArticle'), array(
            'url' => $url,
            'title' => $title,
        ));
    }
}